<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/4/23  14:34
 */
namespace app\admin\Controller;
use think\Controller;
require __DIR__.'/Common.php';

class Credit extends Common
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 荣誉成就列表
     */
    public function index()
    {
        $pageParam = ['query' =>[]];

        $where = '';
        $title = input('title');
        if( $title != ''){
            $where['title'] = array('like','%'.$title.'%');
            $pageParam['query']['title'] = $title;
        }

        $type = input('type')?input('type'):'';
        if($type != ''){
            $where['type'] = $type;
            $pageParam['query']['type'] = $type;
        }

        $data = db('credit')
            ->where($where)
            ->order('id desc')
            ->paginate(10, false, $pageParam)
            ->each(function($item,$key){
                $item['img'] = unserialize($item['img']);
                return $item;
            });
        $page = $data->render();

        $this->assign('page', $page);
        $this->assign('title', $title);
        $this->assign('type', $type);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     *添加表单
     */
    public function add_credit()
    {
        return $this->fetch();
    }

    /**
     * 编辑、添加
     */
    public function edit_credit()
    {
        $id = input('id');
        $credit = db('credit');
        if(request()->isPost())
        {
            $data['title'] = input('title');
            $data['type'] = input('type');
            $data['content'] = input('content');
            $files = request()->file('image');
            if($files)
            {
                $img = '';
                foreach($files as $file){
                    // 移动到框架应用根目录/public/uploads/ 目录下
                    $info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
                    if($info){
                        // 成功上传后 获取上传信息

                        $img[]= $info->getSaveName();

                    }else{
                        // 上传失败获取错误信息
                        echo $file->getError();
                    }
                }
                $data['img'] = serialize($img);
                if($id)
                {
                    //修改删除图片
                    $da=db('credit')->where('id', $id)->find();
                    if(!$da) $this->error('数据不存在');
                    $pics = unserialize($da['img']);
                    foreach($pics as $k=>$v){
                        $pic = ROOT_PATH . 'public/static/uploads/'.$pics[$k];
                        unlink($pic);
                    }
                }
            }

            if($id)
            {
                //修改
                $res = $credit->where('id',$id)->update($data);
                if($res){$this->success('操作成功', url('credit/index'));}else{$this->success('操作失败');}

            }else{
                //添加
                $res = $credit->insert($data);
                if($res){$this->success('操作成功', url('credit/index'));}else{$this->success('操作失败');}
            }
        }

        $data = $credit->where('id', $id)->find();
        $data['img'] = unserialize($data['img']);
//        halt($data);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del_credit(){
        $id = input("id");
//        $this->success($id);
        if(!$id) $this->error('参数有误');
        $da=db('credit')->where('id', $id)->find();
        if(!$da) $this->error('数据不存在');
        if($da['img']){
            $pics = unserialize($da['img']);
            foreach($pics as $k=>$v){
                $pic = ROOT_PATH . 'public/static/uploads/'.$pics[$k];
                unlink($pic);
            }
        }

        $res=db('credit')->where("id='$id'")->delete();
        if(!$res) $this->error('删除失败');
        $this->success("删除成功!",url('credit/index'));
    }
}
